package com.tanhua.admin.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.domain.Log;
import com.tanhua.model.dto.LogDto;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author wjs
 * @version 1.0
 * @date 2022/1/5 0005 下午 6:44
 */
@Component
public class LogListener {

    @Autowired
    private LogMapper logMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "tanhua.log.queue", durable = "true"),
            exchange = @Exchange(value = "tanhua.log.exchange", type = ExchangeTypes.TOPIC),
            key = {"log.*"}
    )
    )
    public void listenCreate(String message) {

        LogDto logDto = JSON.parseObject(message, LogDto.class);
        //1、获取数据
        Long userId = logDto.getUserId();
        String date = logDto.getDate();
        String objId = logDto.getObjId();
        String type = logDto.getType();
        //2、保存到数据库
        Log log = new Log(userId, date, type);
        logMapper.insert(log);

    }
}
